﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ALDIOrganizer
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>
    /// Password hashes are hard-coded, because this class only provides
    /// stub functionality!!
    /// Obviously, user logins and password hashes would normally be retrieved
    /// from some sort of a 
    /// </remarks>
    [Obsolete]    
    public static class UsersStub
    {
        private static readonly byte[] AdminHash;
        private static readonly byte[] AssistantHash;
        private static readonly List<User> UsersDatabase;
        static UsersStub()
        {
            /* why hard-coded??
             * see the Remarks section On the top of this class declaration */
            AdminHash = new byte[]
                {   33,  35,  47,  41,
                   122,  87, 165, 167,
                    67, 137,  74,  14,
                    74, 128,  31, 195 };
            AssistantHash = new byte[] { 245, 73, 205, 115,
                246, 148, 170, 111,
                85, 65, 180, 174,
                48, 137, 78, 234 };
            UsersDatabase = new List<User>();

            var user1 = new User(
                -1,
                "admin",
                AdminHash,
                User.FullPrivileges);
            var user2 = new User(
                2,
                "assistant",
                AssistantHash,
                UserPrivilege.ViewingRequests);
            UsersDatabase.Add(user1);
            UsersDatabase.Add(user2);
        }

        /// <summary>
        /// Compares two byte arrays (hashes) by value
        /// </summary>
        /// <remarks>
        /// Obviously comparing two arrays directly would not work -
        /// array is a reference type (even if it is an array of value types, as byte is),
        /// so { 3, 4, 5 } can be seen as different from another { 3, 4, 5 }.
        /// </remarks>        
        static bool DoHashesMatch(
            byte[] pHashA,
            byte[] pHashB)
        {            
            if ((pHashA == null | pHashB == null) ||
                (pHashA.Length != pHashB.Length))
            {
                return false;
            }            
            var index = 0;
            return pHashA.All(byteValue => byteValue == pHashB[index++]);
        }
    
        /// <summary>
        /// Returns specified user from the database (if exists)
        /// </summary>
        /// <param name="pLogin"></param>
        /// <param name="pHash"></param>
        /// <remarks>
        /// If specified user does not exist (invalid _login and/or password), null is returned.
        /// </remarks>
        public static User GetUser(
            string pLogin,
            byte[] pHash)
        {            
            return (from user in UsersDatabase
                    where user.Login == pLogin &
                    DoHashesMatch(
                    user.GetPasswordHash,
                    pHash)
                    select user).FirstOrDefault();
        }
    }
}
